package com.lagou.tool;

import com.lagou.model.RequestHeader;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class HeaderUtil {

    private static final Map<String,String> LANGUAGE_COMPATIABLE = new HashMap<String, String>(){
        {
            put("en_US".toLowerCase(),"en_US");
            put("zh_CN".toLowerCase(),"en_US");
        }
    };

    public static final String ZH_CN = "zh_CN";
    public static final String EN_US = "en_US";
    private static ThreadLocal<RequestHeader> requestHeader = new ThreadLocal<>();

    public static void setRequestHeader(RequestHeader requestHeader){
        if (requestHeader != null){
            remove();
        }

        HeaderUtil.requestHeader.set(requestHeader);
    }

    private static void remove() {
        requestHeader.remove();
    }

    public static String getTocken(){
        return HeaderUtil.requestHeader.get().getTocken();
    }

    public static String getEmpNo(){
        return HeaderUtil.requestHeader.get().getEmpno();
    }

    public static String getLanguage(){
        if (HeaderUtil.requestHeader.get()==null){
            log.error("no header");
            return ZH_CN;
        }
        String lang = HeaderUtil.requestHeader.get().getLang();

        if (lang!=null){
            return LANGUAGE_COMPATIABLE.getOrDefault(lang.toLowerCase(),lang);
        }else {
            log.error("no header");
            return ZH_CN;
        }

    }

}

